package game;

import util.SuperArray;

import javax.swing.*;

/**
 * 猜数字游戏的界面
 */
public class GameUI {
    private Computer computer = new Computer();
    private Player player = new Player();

    public void process(){
        SuperArray systemNumber = computer.getSystemNumber();
        for (int i = 0;i < 5;i++){
            SuperArray playerNumber = player.inputNumber();
            // 比较两个数组是否相等
            String result = this.compare(systemNumber,playerNumber);
            if("4A0B".equals(result)){
                JOptionPane.showMessageDialog(null,"恭喜你猜对了");
                System.exit(0);
            }else{
                JOptionPane.showMessageDialog(null,result);
            }
        }
        JOptionPane.showMessageDialog(null,"5次机会用完，您都没猜对");
    }

    /**
     * 比较两个数组的值是否相等
     * @param systemNumber
     * @param playerNumber
     * @return 比较的结果，以_A_B表示
     */
    public String compare(SuperArray systemNumber,SuperArray playerNumber){
        int a = 0;
        int b = 0;
        for(int i = 0;i < systemNumber.size();i++){
            for (int j = 0;j < playerNumber.size();j++){
                if(systemNumber.get(i) == playerNumber.get(j)){
                    if(i == j){
                        a++;
                    }else{
                        b++;
                    }
                }
            }
        }
        return a+"A"+b+"B";
    }
}
